<?php
namespace jy_yiliao;
defined('BY_JYA') or exit('error');
/*
	在线问诊
*/
class online extends \plugin{
	public function upworker($workerid){
		if(empty($workerid)){
			exi('缺少必要参数.','error');
		}
		$params=array('uniacid'=>$_SESSION['uniacid'],'id'=>$workerid);
		$worker=pdo_get('jy_yiliao_worker',$params);
		if($worker['mid']){
			return $worker['mid'];
		}
		//自动注册会员
		$new_member=array(
					'uniacid'=>$_SESSION['uniacid'],
					'from_os'=>1,
					'createtime'=>TIMESTAMP,
					'unionid'=>$_SESSION['unionid'],
					'nickname'=>$fans['nickname']?:'',//留空好判断获取
					'username'=>'jy_yiliao_'.random(10),
		);
		$uid=model('core/member')->register($new_member);
		pdo_update('jy_yiliao_worker',array('mid'=>$uid),$params);
		return $uid;
	}
	public function getchatid($mid,$getmid){
		global $_AR;
		if(empty($mid) || empty($getmid)){
			exi('缺少必要参数','error');
		}
		$chats = pdo_getall('jy_yiliao_chatlist',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
		$getchats = pdo_getall('jy_yiliao_chatlist',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$getmid),array('chatid'),'chatid');
		$getchats=array_keys($getchats);
		foreach($chats as $k=>$v){
			if(in_array($v['chatid'],$getchats)){
					$chatid = $v['chatid'];
			}else{
				unset($chats[$k]);
			}
		}
		if(empty($chatid)){
			pdo_insert('jy_yiliao_chat',array('uniacid'=>$_SESSION['uniacid']));
			$chatid = pdo_insertid();
			if($chatid){
				$setdata=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid,'getmid'=>$getmid,'lasttime'=>TIMESTAMP,'chatid'=>$chatid);
				$getdata=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$getmid,'getmid'=>$mid,'lasttime'=>TIMESTAMP,'chatid'=>$chatid);
				pdo_insert('jy_yiliao_chatlist',$setdata);
				pdo_insert('jy_yiliao_chatlist',$getdata);
			}
		}
		return $chatid;
	}
	public function add_feelog($params,$uid=''){
		$uid=$uid?$uid:$_SESSION['uid'];
		$isadd=$this->pdo_get('linelog',array('day'=>$params['day'],'worker_id'=>$params['worker_id'],'mid'=>$params['mid']?:$uid,'uniacid'=>$_SESSION['uniacid']));
		if($isadd['id']){
			exi('您已预约该大夫在线问诊！','error','/online.log/');
		}
		$order=array(
			'uniacid'=>$_SESSION['uniacid'],
			'logsn'=>get_ordersn($this->tablename('linelog'),'logsn'),
			'mid'=>$params['mid']?:$uid,
			'day'=>$params['day'],
			'worker_id'=>intval($params['worker_id']),
			'fee'=>$params['fee'],
			'ispay'=>$params['ispay'],
			'status'=>$params['status'],
			'createtime'=>TIMESTAMP,
		);
		$this->pdo_insert('linelog',$order);
		$log_id=$this->pdo_insertid();
		$this->model('worker')->upmy_user($order['mid'],$order['bodyid'],$order['worker_id']);
		return $log_id;
	}
	public function get_order($order_id){
		if($this->order && $this->order['id']==$order_id){
			return $this->order;
		}
		$this->order=$this->get('linelog',array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
		return $this->order;
	}
	//支付
	public function order_pay($order_id,$params,$url=''){
		$this->get_order($order_id);
		if(!$this->order || $this->order['ispay']){
			$this->pdo_update('linelog',array('status'=>1),array('id'=>$order_id));
			exi('订单不存在或者已支付！','error');
		}
		$update=array(
				'ispay'=>1,
				'status'=>1,
				'paytype'=>$params['paytype'],
				'day'=>date('Y-m-d'),
		);
		$this->pdo_update('linelog',$update,array('id'=>$order_id));
		return true;
	}
	public function addchatlog($chatmsg,$chatid,$getmid,$type=1,$audiotime=0,$setmid=0){
		$chatmsg = trim($chatmsg);
		if(empty($chatmsg)){
			$return['error'] = 1;
			$return['msg'] = '请输入对话内容！';
			exi($return);
		}
		if(empty($chatid)){
			$return['error'] = 1;
			$return['msg'] = '会话丢失！';
			exi($return);
		}
		$getmid = intval($getmid);
		if(empty($_SESSION['uid'])){
			$return['error'] = 1;
			$return['msg'] = '请先登录会员！';
			exi($return);
		}else{
			$data['setmid'] = $setmid?:$_SESSION['uid'];
			$jqruserid = $data['setmid'];
			$data['nickname'] = empty($_SESSION['member']['nickname']) ? '匿名用户' : $_SESSION['member']['nickname'];
			$data['avatar'] =  empty($_SESSION['member']['avatar']) ? tomedia($setting['nologo']) : $_SESSION['member']['avatar'];
		}
		$data['getmid'] = $getmid;
		$data['createtime'] = TIMESTAMP;
		$data['msg'] = $chatmsg;
		$data['uniacid'] = $_SESSION['uniacid'];
		$data['chatid'] = intval($chatid);
		$type = intval($type);
		$data['type'] = $type;
		$data['audiotime'] = intval($audiotime/1000);
		pdo_insert('jy_yiliao_chatlog',$data);
		$logid = pdo_insertid();
		$chat = pdo_get('jy_yiliao_chatlist',array('chatid'=>$data['chatid'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$data['setmid']));
		$passtime = TIMESTAMP-$chat['lasttime'];
		$kefuremindtime = $setting['kefuremindtime'] ? $setting['kefuremindtime'] :120;
		if($passtime > $kefuremindtime){
			if($type == 4){
					$tplcon = $data['nickname'].'发送了图片';
			}elseif($type == 3){
						$tplcon = $data['nickname'].'发送了语音';
			}elseif($type == 7){
						$tplcon = $data['nickname'].'发送文件';
			}else{
				if(strpos($data['msg'],'span class=')){
								$tplcon = $data['nickname'].'发送了表情';
				}else{
								$tplcon = $data['msg'];
				}
			}
			$tplcon=strip_tags(str_replace('&nbsp;','',$tplcon));
			//$tplcon = cfc('tools')->filterword($tplcon,$setting['filterword']);
			$kefumid = pdo_get('core_fans',array('uniacid'=>$_SESSION['uniacid'],'openid'=>$_SESSION['openid']),array('mid'));
			if(!empty($setting['tpl_kefu']) && $setting['isremind']){
				$url = custom_url('jy_yiliao/online.index//getmid='.$data['setmid'],true);
				$topcolor = '#980000';
				$msg = array(
									'first' => array(
										'value' => $data['nickname'].'向您发了信息！',
										'color' => '#990000'
									),
									'keyword1' => array(
										'value' => $tplcon,
										'color' => '#ff510'
									),
									'keyword2' => array(
										'value' => "点击此消息尽快回复",
										'color' => '#ff510'
									),
									'remark' => array(
										'value' => '咨询时间：'.date("Y-m-d H:i:s",TIMESTAMP),
										'color' => '#ff510'
									),							
				);
				$res = cfc('account')->sendTplNotice($getmid,$setting['tpl_kefu'],$msg,$url,$topcolor);
			}else{
				$url = custom_url('jy_yiliao/online.index//getmid='.$data['setmid'],true);
				$concon = $data['nickname'].'向你发了信息！内容:'.$tplcon.'。';
				$msg = array();
				$msg['title'] = '新消息提醒';
				$msg['description'] = $concon;
				$msg['url'] = $url;
				$msg['picurl'] = tomedia($_SESSION['member']['avatar']);
				$news[] = $msg;
				$senddata['msgtype'] = 'news';
				$senddata['news']['articles'] = $news;
				$res = cfc('account')->sendCustomNotice($senddata,$getmid);
			}
		}
		$update=array('meisdel'=>0,'getisdel'=>0,'lastmsg'=>$chatmsg,'msgtype'=>$type,'lasttime'=>TIMESTAMP);
		pdo_update('jy_yiliao_chatlist',$update,array('chatid'=>$data['chatid'],'mid'=>$data['setmid'],'uniacid'=>$_SESSION['uniacid']));
		$return['error'] = 0;
		$audiotime = $data['type'] == 3 ? $data['audiotime'].'\'\' ' : '';
		$audioleng = $data['audiotime'] >2 ? $data['audiotime']:'2';
		$datetime = date("Y-m-d H:i:s",$data['time']);
		$getmemberlogo=$this->session('getmemberlogo');
		$memberlogo=$this->session('memberlogo');
		if($_SESSION['os']=='windows'){
						if($type == 1){
								$html .='<div class="message_box from_self"><img class="mssage_box_pic" src="'.$memberlogo.'" style="display:inline-block;"/><div class="message_content"><div class="message_content_inner"><p>'.$chatmsg.'</p></div></div></div>';
						}elseif($type == 3){
							$chatmsg=tomedia($chatmsg);
							if($v['setmid'] != $_SESSION['uid']){
									$playhtml = '<span ng-show="pagedata.playon == 1" ng-click="pagedata.playon =0" style="display:none;width:'.$v['audioleng'].'rem;text-align:right" class="text kefu-playon"><img src="../core/resource/images/audioplaying.gif"/></span><span ng-show="pagedata.playon !=1" ng-click="pagedata.playon =1" style="width:'.$v['audioleng'].'rem;text-align:right" class="text icon jinyunicon icon-shengbo"></span>';
									$html .='<div onclick="playaudio(\''.$chatmsg.'\');" class="r clearfix"><img class="pic"/><span>'.$v['audiotime'].'\'\'</span>'.$playhtml.'</div>';
							}else{
									if($v['isread'] != 1){
										$unread = '<span class="unread"></span>';
									}else{
										$unread ='';
									}
									$playhtml = '<span ng-show="pagedata.playon !=1" ng-click="pagedata.playon =1" style="width:'.$v['audioleng'].'rem" class="text icon jinyunicon icon-shengbo"></span>';
									$html .='<div onclick="playaudio(\''.$chatmsg.'\',\'\','.$v['id'].');" class="l clearfix"><img class="pic"/>'.$playhtml.'<span>'.$v['audiotime'].'\'\'</span>'.$unread.'</div>';
							}
							$html .= $htmltime;
						}elseif($type == 4){
							$chatmsg=tomedia($chatmsg);
								$html .='<div class="message_box from_self"><img class="mssage_box_pic" src="'.$memberlogo.'" style="display:inline-block;"/><div class="message_content"><p onclick="followewm(\''.$chatmsg.'\');" class="message_content_inner media"><img src="'.$chatmsg.'" style="width:100%;"/></p></div></div>';
						}elseif($type == 7){
							$fileurl=tomedia($chatmsg);
							$pathinfo=pathinfo($chatmsg);
							$fileicon=$pathinfo[extension];
							$filename=pdo_getcolumn('core_attachment',array('uniacid'=>$_SESSION['uniacid'],'attachment'=>$chatmsg),'title');
							$filename=$filename.'.'.$pathinfo[extension];
							if(strpos($fileicon,'xls')){
								$fileicon='icon-exl';
							}elseif(strpos($fileicon,'pd')){
								$fileicon='icon-pdf';
							}elseif(strpos($fileicon,'do')){
								$fileicon='icon-exl';
							}elseif(strpos($fileicon,'zip') || strpos($fileicon,'rar')){
								$fileicon='icon-zip';
							}else{
								$fileicon='icon-copy';
							}
							$filesize=filesize(ROOT_AT.'/'.$chatmsg)/1000;
							$filesize=$filesize.'kb';
							$html .='<div class="message_box from_self"><img class="mssage_box_pic" src="'.$memberlogo.'" style="display:inline-block;"/><div class="message_content"><div class="message_content_inner"><a href="'.$fileurl.'" target="_blank" class="fui-cell"><div class="text"><div>'.$filename.'</div><div style="font-size:12px">'.$filesize.'</div></div><div class="icon"><i class="icon '.$fileicon.'"></i></div></a></div></div></div>';
						}
		}else{
			if($type == 1){
				$html .='<div class="r clearfix"><img class="pic"/><p class="text">'.$chatmsg.'</p></div>';
			}elseif($type == 3){
				$playhtml = '<span ng-click="pagedata.playon ='.$logid.'" style="width:'.$audioleng.'rem;text-align:right" class="text icon jinyunicon icon-shengbo"></span>';
				$html .='<div onclick="playaudio(\''.$chatmsg.'\');" class="r clearfix"><img class="pic"/><span>'.$audiotime.'</span>'.$playhtml.'</div>';
			}elseif($type == 4){
				$chatmsg = tomedia($chatmsg);
				$html .='<div class="r clearfix"><img class="pic"/><p onclick="followewm(\''.$chatmsg.'\');"  class="textimg"><img style="max-width:100%" src="'.$chatmsg.'"/></p></div>';
			}elseif($type == 7){
				$fileurl=tomedia($chatmsg);
				$pathinfo=pathinfo($chatmsg);
				$fileicon=$pathinfo[extension];
				$filename=pdo_getcolumn('core_attachment',array('uniacid'=>$_SESSION['uniacid'],'attachment'=>$chatmsg),'title');
				$filename=$filename.'.'.$pathinfo[extension];
				if(strpos($fileicon,'xls')){
					$fileicon='icon-exl';
				}elseif(strpos($fileicon,'pd')){
					$fileicon='icon-pdf';
				}elseif(strpos($fileicon,'do')){
					$fileicon='icon-exl';
				}elseif(strpos($fileicon,'zip') || strpos($fileicon,'rar')){
					$fileicon='icon-zip';
				}else{
					$fileicon='icon-copy';
				}
				$filesize=filesize(ROOT_AT.'/'.$chatmsg).'kb';
				$html .='<div class="r clearfix"><img class="pic"/><div class="text"><a href="'.$fileurl.'" target="_blank" class="fui-cell"><div class="text"><div>'.$filename.'</div><div style="font-size:12px">'.$filesize.'</div></div><div class="icon"><i class="icon '.$fileicon.'"></i></div></a></div></div>';
			}
		}
		$return['htmlmsg']=$html;
		$return['audiotime']=$audiotime;
		$return['audioleng']=$audioleng;
		$return['id']=$logid;
		return $return;
	}
}